<?php

class Admin_UserController extends Zend_Controller_Action
{
    public function indexAction()
    {

    }

    /**
     * 用户管理
     */
    public function listAction()
    {
        $pageNum = 1;
        if ($this->_getParam("page")) {
        	$pageNum = $this->_getParam("page");
        }
        $perPage = 20;
        if ($this->_getParam("perPage")) {
        	$perPage = $this->_getParam("perPage");
        }
        $where = array();
        $order = array(array('uid', 'desc'));
        $userModel = new Model_User();
        $select = $userModel->fetchAll($where, $order);
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage($perPage);
        $paginator->setCurrentPageNumber($pageNum);
        $rList = array();
        $roleModel = new Model_Role();
        $roleList = $roleModel->dataList()->toArray();
        foreach ($roleList as $role){
            $rList[$role['rid']] = $role['rname_cn'];
        }
        $this->view->roleList = $rList;
        $this->view->paginator = $paginator;
    }

    /**
     * 角色管理，角色列表
     */
    public function roleAction()
    {
        $role = new Model_Role();
        $roles = $role->dataList();
        $this->view->roles = $roles->toArray();
    }

    /**
     * 角色添加
     */
    public function roleAddAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        if($this->_request->isPost()){
            $role = new Model_Role();
            $data = $this->_getAllParams();
            if(!empty($data['rname_cn'])){
                $chineseSpell = new Hm_ChineseSpell();
                $data['rname'] = $chineseSpell->getFullSpell($data['rname_cn'],'_');
                $data['rtype'] = 'user';
                $role->save($data);
            }else{
                throw new Exception('角色名不能为空！');
            }
        }
        $this->_helper->redirector('role');
    }

    /**
     * 角色删除
     */
    public function roleDeleteAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $rid = $this->_getParam('id');
        if(!$rid){
            throw new Exception('没有指定删除的角色！');
        }
        $roleModel = new Model_Role();
        $role = $roleModel->find($rid);
        if($role){
            if($role->rtype == 'system'){
                throw new Exception('系统用户不允许删除！');
            }else{
                $role->drop();
            }
        }else{
            throw new Exception('您要删除的角色不存在，或已经被删除！');
        }
        $this->_helper->redirector('role');
    }

    /**
     * 角色编辑
     */
    public function roleEditAction()
    {
        $rid = $this->_getParam('id');
        if(!$rid){
            throw new Exception('没有指定编辑的角色！');
        }
        $roleModel = new Model_Role();
        $role = $roleModel->find($rid);
        if($role){
            if($this->_request->isPost()){
                $data = $this->_getAllParams();
                if(!empty($data['rname_cn'])){
                    $role->setData($data);
                    $role->save();
                }else{
                    throw new Exception('角色名不能为空！');
                }
                $this->_helper->redirector('role');
            }
            $this->view->role = $role;
        }else{
            throw new Exception('您要编辑的角色不存在，或已经被删除！');
        }
    }

    /**
     * 修改角色权限
     */
    public function rolePermissionsAction()
    {
        $rid = $this->_getParam('id');
        if(!$rid){
            throw new Exception('没有指定编辑权限的角色！');
        }
        $roleModel = new Model_Role();
        $role = $roleModel->find($rid);
        if(!$role){
            throw new Exception('您要编辑权限的角色不存在，或已经被删除！');
        }
        $permissionModel = new Model_Permissions();
        if($this->_request->isPost()){
            $resId = $this->_getParam('resid');
            $permissionModel->deleteRolePermission($rid);
            foreach ($resId as $id) {
                $data = array(
                    'access' => 'allow' ,
                    'role_id' => $rid ,
                    'res_id' => $id
                );
                $permissionModel->save($data);
            }
            Model_Acl::refresh(Zend_Registry::get('cache'));
            $this->_helper->redirector('role');
        }else{
            $resources = array();
            $resourceCategory = new Model_ResourcesCategory();
            $categories = $resourceCategory->dataList();
            foreach ($categories as $category){
                if($category){
                    $resources[$category->rcname] = $category->findDependentRowset('Model_DbTable_Resources')->toArray();
                }
            }
            $rolePermissions = $permissionModel->getPermissionsByRoleId($rid,false);
            $this->view->role = $role;
            $this->view->resources = $resources;
            $this->view->permissions = $rolePermissions;
        }
    }
}